/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.example.ui.behavior
{

import flash.events.Event;
import flash.events.MouseEvent;

import org.example.ui.component.ILoginComponent;
import org.example.ui.model.LoginComponentModelCore;
import org.teotigraphix.tka.patterns.behavior.SparkComponentBehaviorCore;

import spark.components.Button;
import spark.components.TextInput;
import spark.primitives.supportClasses.TextGraphicElement;

[ComponentHandler(event="loggingInMessageChanged", handler="loggingInMessageHandler")]
[ComponentHandler(event="loggedInMessageChanged", handler="loggedInMessageHandler")]
[ComponentHandler(event="loginLabelChanged", handler="loginLabelHandler")]
[ComponentHandler(event="passwordLabelChanged", handler="passwordLabelHandler")]

/**
 * The <code>LoginComponent</code>'s behavioral core.
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 */
public class LoginComponentBehaviorCore 
	   extends SparkComponentBehaviorCore
{
	//--------------------------------------------------------------------------
	//
	//  Public ModelAlias :: Variables
	//
	//--------------------------------------------------------------------------
	
	[ModelAlias]
	
	/**
 	 * The <code>LoginComponent</code>'s model core.
	 */
	public var coreModel:LoginComponentModelCore;
		
	//--------------------------------------------------------------------------
	//
	//  Public ComponentAlias :: Variables
	//
	//--------------------------------------------------------------------------
	
	[ComponentAlias]
	
	/**
	 * The login component.
	 */	
	public var login:ILoginComponent;
	
	//--------------------------------------------------------------------------
	//
	//  Public SkinPartAlias :: Variables
	//
	//--------------------------------------------------------------------------
	
	[SkinPartAlias]
	[SkinPartHandler(event="click", handler="loginButton_clickHandler")]
		
	/**
	 * The login component's button part.
	 */
	public var loginButton:Button;
	
	[SkinPartAlias]
	
	/**
	 * The login component's username input part.
	 */	
	public var loginInput:TextInput;
	
	[SkinPartAlias]
	
	/**
	 * The login component's password input part.
	 */	
	public var passwordInput:TextInput;
	
	[SkinPartAlias()]
	
	/**
	 * The login component's username label part.
	 */		
	public var loginInputLabel:TextGraphicElement;
	
	[SkinPartAlias(required="false")]
	
	/**
	 * The login component's password label part.
	 */	
	public var passwordInputLabel:TextGraphicElement;
	
	[SkinPartAlias(required="false")]
	
	/**
	 * The login component's status message part.
	 */		
	public var loginMessage:TextGraphicElement;	
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Creates a new <code>LoginComponentBehaviorCore</code> instance.
	 */
	public function LoginComponentBehaviorCore()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Public :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	override public function validateProperties():void
	{
		super.validateProperties();
		
 		coreModel.invalidateLoginMessage();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	override protected function partAdded(partName:String, instance:Object):void 
	{
		super.partAdded(partName, instance);
		
		if (instance === loginButton) 
		{
		}
		else if (instance === loginInput) 
		{
		}
		else if (instance === passwordInput) 
		{
			passwordInput.displayAsPassword = true;
		}
		else if (instance === loginInputLabel) 
		{
			loginInputLabel.text = login.loginLabel;
		}
		else if (instance === passwordInputLabel) 
		{
			passwordInputLabel.text = login.passwordLabel;
		}
		
	}
	
	/**
	 * @private
	 */
	override protected function partRemoved(partName:String, instance:Object):void 
	{
		if (instance === loginButton)
		{
		}
	}	
	
	//--------------------------------------------------------------------------
	//
	//  component_handler :: Handlers
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	component_handler function loggingInMessageHandler(event:Event):void
	{
		uicomponent.invalidateProperties();
	}
	
	/**
	 * @private
	 */
	component_handler function loggedInMessageHandler(event:Event):void
	{
		uicomponent.invalidateProperties();
	}
	
	/**
	 * @private
	 */
	component_handler function loginLabelHandler(event:Event):void
	{
		if(loginInputLabel)
			loginInputLabel.text = login.loginLabel;
	}
	
	/**
	 * @private
	 */
	component_handler function passwordLabelHandler(event:Event):void
	{
		if(passwordInputLabel)
			passwordInputLabel.text = login.passwordLabel;
	}
	
	//--------------------------------------------------------------------------
	//
	//  skinpart_handler :: Handlers
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Event handler for when the login button is clicked.
	 *
	 * @param evt associated event object
	 */
	skinpart_handler function loginButton_clickHandler(evt:MouseEvent):void 
	{
		// if the component is not logging in and not logged in already
		if(!coreModel.loggingIn && !coreModel.loggedIn) 
		{
			// fires LoginEvent.LOGIN event
			coreModel.loggingIn = true;
			
			loginButton.drawFocus(false);
		}
	}
}
}